package com.tj.jinzao.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.tj.jinzao.R;
import com.tj.jinzao.activity.StartLiveActivity;
import com.tj.jinzao.adapter.LiveListItemAdapter;
import com.tj.jinzao.base.BaseDataListFragment;
import com.tj.jinzao.base.ConstantApp;
import com.tj.jinzao.base.RecyclerViewGridAdapter;
import com.tj.jinzao.callback.LiveView;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.LiveBean;
import com.tj.jinzao.model.User;
import com.tj.jinzao.presenter.LivePresenter;
import com.tj.jinzao.utils.MessageUtil;
import com.tj.jinzao.utils.ToastUtils;
import com.tj.jinzao.utils.Utils;
import com.tj.jinzao.widgets.ChoosePassWordDialog;
import com.tj.jinzao.widgets.EmptyView;

import java.util.HashMap;
import java.util.List;

import io.agora.rtc.Constants;


/**
 * 直播列表
 * Created by wjy on 2019/1/1.
 */
public class LiveListFragment extends BaseDataListFragment<List<LiveBean>, LivePresenter, LiveBean> implements LiveView<List<LiveBean>> {
    private ChoosePassWordDialog mDialog;

    public static LiveListFragment newInstance(int position) {
        LiveListFragment fragment = new LiveListFragment();
        if (fragment != null) {
            Bundle args = new Bundle(3);
            args.putString(ARG_TAG_ID, String.valueOf(position));
            args.putInt(ARG_LOAD_ID, 1);
            args.putInt(ARG_LAYOUT_RES_ID, R.layout.base_fragment_item_layout);
            fragment.setArguments(args);
        }
        return fragment;
    }

    @Override
    public void onRequestNoData(String message) {
        setEmptyDataView();
    }
    private User user;

    @Override
    protected LivePresenter createPresenter() {
        user= UserManager.getInstance().getUser();
        return new LivePresenter(this);
    }

    @Override
    protected void createView(LayoutInflater inflater, View view) {
    }

    @Override
    protected RecyclerView.Adapter createAdapter() {
        RecyclerViewGridAdapter adapter = new RecyclerViewGridAdapter(mContext, 2, new LiveListItemAdapter(mContext, mData));
        int padding = Utils.dpToPx(12, getResources());
        int left = Utils.dpToPx(16, getResources());
        int itemWidth = (Constant.SCREEN_WIDTH - padding - left * 2) / 2;
        int itemHeight = itemWidth * 252 / 163;
        adapter.setItemSize(itemWidth, itemHeight);
        adapter.setMargin(left, left, 0, padding, padding);
        return adapter;
    }

    @Override
    public void onResume() {
        super.onResume();
        if(mPresenter!=null){
            mPageNo=1;
            requestData();

        }

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(mPresenter!=null){
            mPageNo=1;
            requestData();

        }

    }


    @Override
    protected void requestData() {
        if(mTag.equals("0")){
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("currentPage", mPageNo);
            hashMap.put("uid", user.getId());
            mPresenter.getRecomedList(hashMap);
        }else if(mTag.equals("1")){
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("currentPage", mPageNo);
            hashMap.put("recommend", "true");
            mPresenter.getLiveList(hashMap);
        }else{
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("currentPage", mPageNo);
            mPresenter.getLiveList(hashMap);

        }
    }

    @Override
    protected void setEmptyDataView() {
        mEmptyView.setVisibility(View.VISIBLE);
        mEmptyView.setState(EmptyView.State.NO_DATA, "没有内容哦");
    }

    @Override
    public void onRequestSuccess(List<LiveBean> articles) {
        super.onRequestSuccess(articles);
        if (mPageNo <= 1) {
            mData.clear();
        }
        mData.addAll(articles);
        showListView();

    }

    @Override
    protected boolean resolveData(List<LiveBean> liveBeans) {
        return false;
    }

    @Override
    protected void initData() {
    }
    private LiveBean datas;
    private int liveId;


    @Override
    protected void itemClicked(int position, final LiveBean data) {
        if(String.valueOf(data.getWebUser().getId()).equals(String.valueOf(user.getId()))){
            ToastUtils.showToast(getActivity(),"不能进入自己的直播间");
            return;
        }
        if(data!=null&&data.getWebUser()!=null&&!data.getLiveStatus().equals("true")){
            ToastUtils.showToast(getActivity(),"该直播尚未开始");
            return;
        }
        mDialog = new ChoosePassWordDialog(getActivity());
        mDialog.show();
        mDialog.setOnClickLinstener(new ChoosePassWordDialog.OnClickLinstener() {
            @Override
            public void onConfig(String passWord) {
                //        Constants.CLIENT_ROLE_AUDIENCE  观众
                datas=data;
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("id", data.getId());
                hashMap.put("password", passWord);
                hashMap.put("loginUid", user.getId());
                liveId=data.getRecord().getId();
                mPresenter.goLive(hashMap);
                mDialog.dismiss();
            }

            @Override
            public void onExit() {
                mDialog.dismiss();
            }
        });

    }


    @Override
    public void onRequestSuccessPage(int currentPage, int allPage, int allSize) {
        mPageNo = currentPage;
        mPageNum = allPage;

    }

    @Override
    public void onReload(Object tag) {

    }

    @Override
    public void goLiveSuccess(LiveBean liveBean) {
            Intent i = new Intent(getActivity(), StartLiveActivity.class);
            i.putExtra(ConstantApp.ACTION_KEY_CROLE, Constants.CLIENT_ROLE_AUDIENCE);
            i.putExtra("user", liveBean.getWebUser());
            i.putExtra("bid", liveBean.getId());
            i.putExtra("idlive", liveId);
            i.putExtra(ConstantApp.ACTION_KEY_ROOM_NAME, String.valueOf(datas.getWebUser().getId()));
            i.putExtra(MessageUtil.INTENT_EXTRA_IS_TYPE, true);
            startActivity(i);


    }

    @Override
    public void goLiveFailed(String msg) {
        ToastUtils.showToast(getActivity(),msg);

    }
}
